#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read<int>()
#define lin read<ll>()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

template <typename T>
T read(){
	T x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e5+10;

ll G,X,Y;
int n;

struct unn{
	ll m,a;
	bool operator < (const unn b)const{return m > b.m;}
}un[N];

ll gcd(ll x,ll y){return !y ? x : gcd(y,x%y);}

ll lcm(ll x,ll y){return x / gcd(x,y) * y;}

ll mul(ll x,ll y,ll mod){x %= mod;y %= mod;ll res=0;for(;y;y>>=1,x=(x+x)%mod)if(y&1)res = (res + x) % mod;return res;}

bool merge(ll x1,ll m1,ll x2,ll m2,ll &x3,ll &m3){
	m3 = lcm(m1,m2); if(m1 < m2) swap(m1,m2),swap(x1,x2); x2 %= m2;
	for(ll i = 0;i < m2;i++){
		if((mul(i,m1,m2) + x1) % m2 == x2) {x3 = (mul(i,m1,m3) + x1) % m3;return 1;}
	}return 0;
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;
	for(int i = 1;i <= n;i++) un[i].m = lin,un[i].a = lin;
	sort(un+1,un+n+1);
	ll x = 0,M = 1;
	for(int i = 1;i <= n;i++){
		if(!merge(x,M,un[i].a,un[i].m,x,M)) return puts("-1"),0;
	}
	printf("%lld\n",x);
	return 0;
}

